<template>
  <div>
    <!--@addTitleClick="addTitleClick"-->
    <common-title
        v-if="businessTableData.componentType.title"
        :componentSetOptions="businessTableData.titleProp"

    />

    <common-tabs
        v-if="businessTableData.componentType.tabs"
        :componentSetOptions="businessTableData.tabsProp"
    />

    <common-table
        v-if="businessTableData.componentType.table"
        :componentSetOptions="businessTableData.tableProp"
    />

    <common-dialog
        v-if="businessTableData.componentType.dialog"
        :componentSetOptions="businessTableData.dialogProp"
        ref='businessCommonDialog'
    />
  </div>
</template>

<script>

export default {
  components: {},
  props: ['businessTableData'],
  data() {
    return {
      initDialogData: null,//存储dialog原始数据，初始化要用
    };
  },
  mounted() {

    //把dialog原始数据存储
    if(this.businessTableData.componentType.dialog){
      this.initDialogData = JSON.parse(JSON.stringify(this.businessTableData.dialogProp.formData));
    }
    //初始化
    if(this.businessTableData.interfaceName.initMethodName!==''){
      this.HandleUtils.handleParent(this.$parent,this.businessTableData.interfaceName.initMethodName)
    }
  },
  methods: {
    //CommonTitle组件 添加账号 按钮点击
    addTitleClick(item) {
      this.businessTableData.dialogProp.formData = JSON.parse(JSON.stringify(this.initDialogData));
      this.businessTableData.dialogProp.dialogTitle = item.name;
      this.businessTableData.dialogProp.isDialog = true;
    },
    //CommonTabs组件 点击搜索 点击tabs状态 时候调用的方法
    tabsClick() {
      if(this.businessTableData.tableProp!=undefined){
        this.businessTableData.tableProp.current_page=1;
      }
      this.HandleUtils.handleParent(this.$parent,this.businessTableData.interfaceName.initMethodName)
    },
    //CommonTable组件
    //编辑按钮
    tableEditClick(row,item){
      this.businessTableData.dialogProp.formData = JSON.parse(JSON.stringify(this.initDialogData));
      this.businessTableData.dialogProp.dialogTitle = item.name;
      let params = {id: row.id};
      this.businessTableData.dialogProp.loading=true;
      this.businessTableData.dialogProp.isDialog = true;
      if(this.businessTableData.interfaceName.oneData!==''){
        this['$'+this.businessTableData.interfaceName.oneData](params).then((result) => {
          this.businessTableData.dialogProp.formData = result;
          this.businessTableData.dialogProp.loading=false;
        })
      }
    },
    //状态按钮 关闭/启动等
    tableStatusClick(row,item){
      let data=this.HandleUtils.handleParams(row);
      data[this.businessTableData.tabsProp.tabsSwitchField]=item.status;
      if(this.businessTableData.interfaceName.status!==''){
        this['$'+this.businessTableData.interfaceName.status](data).then(() => {
          this.$message.success('关闭成功');
          this.HandleUtils.handleParent(this.$parent,this.businessTableData.interfaceName.initMethodName)
        })
      }
    },
    //删除按钮
    tableDelClick(row){
      this.$confirm('此操作将永久删除, 是否继续?', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        let data = {
          id: row.id
        }
        if(this.businessTableData.interfaceName.del!==''){
          this['$'+this.businessTableData.interfaceName.del](data).then(() => {
            this.$message.success('删除成功');
            this.HandleUtils.handleParent(this.$parent,this.businessTableData.interfaceName.initMethodName)
          })
        }
      }).catch(() => {
        this.$message.info('已取消删除');
      });
    },
    // 分页按钮
    tablePageClick(page) {
      this.businessTableData.tableProp.current_page = page
      this.HandleUtils.handleParent(this.$parent,this.businessTableData.interfaceName.initMethodName)
    },
    //CommonDialog组件
    //取消按钮
    dialogCancel(){
      this.businessTableData.dialogProp.isDialog = false;
    },
    //确定按钮
    dialogDetermine(){
      this.$refs['businessCommonDialog'].$refs[this.businessTableData.dialogProp.formRef].validate((valid) => {
        if (valid) {
          let data = this.HandleUtils.handleParams(this.businessTableData.dialogProp.formData);
          if (this.businessTableData.dialogProp.formData.id == '') {
            if(this.businessTableData.interfaceName.add!==''){
              this['$'+this.businessTableData.interfaceName.add](data).then(() => {
                this.$message.success('提交成功');
                this.businessTableData.dialogProp.isDialog = false;
                this.HandleUtils.handleParent(this.$parent,this.businessTableData.interfaceName.initMethodName)
              })
            }
          } else {
            if(this.businessTableData.interfaceName.edit!=='') {
              this['$'+this.businessTableData.interfaceName.edit](data).then(() => {
                this.$message.success('修改成功');
                this.businessTableData.dialogProp.isDialog = false;
                this.HandleUtils.handleParent(this.$parent,this.businessTableData.interfaceName.initMethodName)
              })

            }

          }
        } else {
          console.log('验证没过!!');
          return false;
        }
      })
    }
  },
};
</script>

<style lang="scss" scoped></style>
